package com.kczd.jinlan.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kczd.jinlan.R;
import com.kczd.jinlan.model.Goods;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 首页特价adapter
 * zhangy
 * Created by Administrator on 2017/5/16.
 */

public class TejiaAdapter extends RecyclerView.Adapter<TejiaAdapter.ViewHolder>  {

    /**
     * ItemClick的回调接口
     * @author zhy
     *
     */
    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }


    private LayoutInflater mInflater;
    private List<Goods> mDatas;

    public TejiaAdapter(Context context, List<Goods> datats)
    {
        mInflater = LayoutInflater.from(context);
        mDatas = datats;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View arg0)
        {
            super(arg0);
        }
        ImageView mImg;
        TextView tejia_name;
        TextView tejia_price;
        TextView tejia_oldprice;
        ImageView gouwuche;
    }

    @Override
    public int getItemCount()
    {
        return mDatas.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
    {
        View view = mInflater.inflate(R.layout.tejia_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);

        viewHolder.mImg = (ImageView) view.findViewById(R.id.tejia_img);
        viewHolder.tejia_price = (TextView) view.findViewById(R.id.tejia_price);
        viewHolder.tejia_name = (TextView) view.findViewById(R.id.tejia_name);
        viewHolder.tejia_oldprice = (TextView) view.findViewById(R.id.tejia_oldprice);

        //文字加横杠
        viewHolder.tejia_oldprice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );

        viewHolder.gouwuche= (ImageView) view.findViewById(R.id.id_gouwuche);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i)
    {
        viewHolder.tejia_name.setText(mDatas.get(i).getTitle());
        viewHolder.tejia_price.setText("¥"+mDatas.get(i).getPrice());
        viewHolder.tejia_oldprice.setText("¥"+mDatas.get(i).getOldprice());
        // 显示图片的配置
        DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.img_loading)
                .showImageOnFail(R.mipmap.img_fail).cacheInMemory(true).cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
        ImageLoader.getInstance().displayImage(mDatas.get(i).getImage(), viewHolder.mImg, options);
        //如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null)
        {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    mOnItemClickLitener.onItemClick(viewHolder.itemView, i);
                }
            });
        }
    }


}
